package http;

import model.ApiConfigDataModel;
import model.ApiDataModel;
import tool.DataReplace;
import tool.GuessContentType;
import tool.MyIo;

import java.io.*;
import java.util.Map;

/**
 * Created by yangxianming on 2015-06-10.
 */
public class PackageMultipartData implements PackageDataImpl{
    ApiDataModel adm = null;
    ApiConfigDataModel acdm = null;
    String boundary = "";

    PackageMultipartData(ApiDataModel adm, ApiConfigDataModel acdm){
        this.adm = adm;
        this.acdm = acdm;
        this.boundary = acdm.config.get("boundary");
    }

    @Override
    public Object packageData(){
        return null;
    }


    public Object packageData(OutputStream from){
        StringBuffer strBuf = new StringBuffer();
        OutputStream out = from;

        for (Map.Entry<String, String> me:adm.parameters.entrySet()){
            String k = me.getKey();
            String v = me.getValue();
            v = DataReplace.replace(acdm, v);
            me.setValue(v);     //将替换后的数据放回对象

            if (k.contains("upLoad|")){
//                这里就是需要上传文件的字段
                String contentType = GuessContentType.getContentType(v);
                k = k.substring(k.indexOf("upLoad|") + 7);  //去掉上传表示字符
                if (v == null) {   //处理没有文件上传时
                    strBuf.append("Content-Disposition: form-data; name=\""+ k +"\"" + "\r\n\r\n");
                    continue;
                }
                File file = new File(v);
                String filename = file.getName();
                strBuf = new StringBuffer();
                strBuf.append("\r\n").append("--").append(boundary).append("\r\n");
                strBuf.append("Content-Disposition: form-data; name=\"" + k + "\"; filename=\"" + filename + "\"\r\n");
                strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
                try {
                    out.write(strBuf.toString().getBytes());
                    out.write(MyIo.dump(new DataInputStream(new FileInputStream(file))));
                    strBuf.delete(0, strBuf.length());
                }catch (Exception e){
                    e.printStackTrace();
                }
//                System.out.println(k);
            }else {
//                这里是普通字段
                strBuf.append("\r\n").append("--").append(boundary).append("\r\n");
                strBuf.append("Content-Disposition: form-data; name=\"" + k + "\"\r\n\r\n");
                strBuf.append(v);
//                System.out.println(strBuf);
                try {
                    out.write(strBuf.toString().getBytes());
                    strBuf.delete(0, strBuf.length());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
//        strBuf.append("\r\n--" + boundary + "--\r\n");
//        System.out.println(strBuf);
        try {
            out.write(("\r\n--" + boundary + "--\r\n").getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }

        return out;
    }
}
